#***************************************************************************
#                                  _   _ ____  _
#  Project                     ___| | | |  _ \| |
#                             / __| | | | |_) | |
#                            | (__| |_| |  _ <| |___
#                             \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################
# Find the nghttp3 library
#
# 此模块用于查找系统中的 nghttp3 库。
# 它会尝试通过 pkg-config 或者手动查找的方式定位 nghttp3 库和头文件。
#
# Input variables:
#
# - `NGHTTP3_INCLUDE_DIR`:   The nghttp3 include directory.
#                            nghttp3 头文件所在的目录。
# - `NGHTTP3_LIBRARY`:       Path to `nghttp3` library.
#                            `nghttp3` 库文件的路径。
#
# Result variables:
#
# - `NGHTTP3_FOUND`:         System has nghttp3.
#                            系统中是否找到了 nghttp3 库。
# - `NGHTTP3_INCLUDE_DIRS`:  The nghttp3 include directories.
#                            nghttp3 头文件所在的目录列表。
# - `NGHTTP3_LIBRARIES`:     The nghttp3 library names.
#                            nghttp3 库文件的名称列表。
# - `NGHTTP3_LIBRARY_DIRS`:  The nghttp3 library directories.
#                            nghttp3 库文件所在的目录列表。
# - `NGHTTP3_PC_REQUIRES`:   The nghttp3 pkg-config packages.
#                            nghttp3 的 pkg-config 包名。
# - `NGHTTP3_CFLAGS`:        Required compiler flags.
#                            编译所需的标志。
# - `NGHTTP3_VERSION`:       Version of nghttp3.
#                            nghttp3 的版本号。

# 设置 nghttp3 的 pkg-config 包名
set(NGHTTP3_PC_REQUIRES "libnghttp3")

# 若启用了使用 pkg-config 查找库，并且未手动指定头文件目录和库文件路径
if(CURL_USE_PKGCONFIG AND
   NOT DEFINED NGHTTP3_INCLUDE_DIR AND
   NOT DEFINED NGHTTP3_LIBRARY)
  # 安静模式查找 PkgConfig 模块
  find_package(PkgConfig QUIET)
  # 使用 pkg-config 检查 nghttp3 库
  pkg_check_modules(NGHTTP3 ${NGHTTP3_PC_REQUIRES})
endif()

# 若通过 pkg-config 找到了 nghttp3 库
if(NGHTTP3_FOUND)
  # 将 NGHTTP3_CFLAGS 中的分号替换为空格
  string(REPLACE ";" " " NGHTTP3_CFLAGS "${NGHTTP3_CFLAGS}")
  # 输出找到 nghttp3 库的状态信息，包含头文件目录和版本号
  message(STATUS "Found NGHTTP3 (via pkg-config): ${NGHTTP3_INCLUDE_DIRS} (found version \"${NGHTTP3_VERSION}\")")
else()
  # 手动查找 nghttp3 的头文件目录，查找的头文件为 nghttp3/nghttp3.h
  find_path(NGHTTP3_INCLUDE_DIR NAMES "nghttp3/nghttp3.h")
  # 手动查找 nghttp3 的库文件，库文件名为 nghttp3
  find_library(NGHTTP3_LIBRARY NAMES "nghttp3")

  # 从缓存中移除 NGHTTP3_VERSION 变量
  unset(NGHTTP3_VERSION CACHE)
  # 若找到了头文件目录，并且 nghttp3/version.h 文件存在
  if(NGHTTP3_INCLUDE_DIR AND EXISTS "${NGHTTP3_INCLUDE_DIR}/nghttp3/version.h")
    # 定义正则表达式，用于匹配 NGHTTP3_VERSION 宏定义
    set(_version_regex "#[\t ]*define[\t ]+NGHTTP3_VERSION[\t ]+\"([^\"]*)\"")
    # 从 nghttp3/version.h 文件中读取匹配正则表达式的行
    file(STRINGS "${NGHTTP3_INCLUDE_DIR}/nghttp3/version.h" _version_str REGEX "${_version_regex}")
    # 使用正则表达式提取版本号
    string(REGEX REPLACE "${_version_regex}" "\\1" _version_str "${_version_str}")
    # 设置 NGHTTP3_VERSION 变量为提取的版本号
    set(NGHTTP3_VERSION "${_version_str}")
    # 移除临时变量
    unset(_version_regex)
    unset(_version_str)
  endif()

  # 包含 FindPackageHandleStandardArgs 模块，用于处理查找结果
  include(FindPackageHandleStandardArgs)
  # 使用标准参数处理查找结果
  find_package_handle_standard_args(NGHTTP3
    REQUIRED_VARS
      NGHTTP3_INCLUDE_DIR
      NGHTTP3_LIBRARY
    VERSION_VAR
      NGHTTP3_VERSION
  )

  # 若手动查找找到了 nghttp3 库
  if(NGHTTP3_FOUND)
    # 设置头文件目录列表
    set(NGHTTP3_INCLUDE_DIRS ${NGHTTP3_INCLUDE_DIR})
    # 设置库文件名称列表
    set(NGHTTP3_LIBRARIES    ${NGHTTP3_LIBRARY})
  endif()

  # 将 NGHTTP3_INCLUDE_DIR 和 NGHTTP3_LIBRARY 标记为高级变量，在 CMake GUI 中默认隐藏
  mark_as_advanced(NGHTTP3_INCLUDE_DIR NGHTTP3_LIBRARY)
endif()
